<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use Hyperf\HttpServer\Annotation\Controller;
use App\Constants\ErrorCode;
use Hyperf\HttpServer\Annotation\PostMapping;
use App\Exception\BussniessException;
use Core\Repository\Admin\PassportRepository;

/**
 * PassportController
 * 通行证相关
 * @package App\Controller\Admin
 * @Controller(prefix="/passport")
 * @property PassportRepository $passportRepo
 */
class PassportController extends BaseController
{
    /**
     * login
     * 处理登录
     * @return \Psr\Http\Message\ResponseInterface
     * @PostMapping(path="login")
     */
    public function login()
    {
        $inputData = $this->request->all();
        $validator = $this->validation->make(
            $inputData,
            [
                'username' => 'required',
                'password' => 'required',
            ],
            [
                'username.required' => '账号不能为空',
                'password.required' => '密码不能为空',
            ]
        );
        if ($validator->fails()){
            $errorMessage = $validator->errors()->first();
            throw new BussniessException(ErrorCode::ERR_EXCEPTION_USER,$errorMessage);
        }
        $data = $this->passportRepo->handleLogin($inputData);

        return $this->response->success($data);
    }

    /**
     * submitLogout
     * 函数的含义说明
     * @return \Psr\Http\Message\ResponseInterface
     *
     * @PostMapping(path="logout")
     */
    public function submitLogout()
    {
        $this->passportRepo->handleLogout();

        return $this->response->success('','退出成功');
    }
}